package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 中新网四川
 * @author Persh
 *
 */
public class ScchinanewsNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://www.sc.chinanews.com.cn/news/[0-9]{4}/[0-9]{4}/[0-9]*.html",
		"http://www.ha.chinanews.com.cn/.*/[0-9]*/[0-9]{4}-[0-9]{2}-[0-9]{2}/news-[0-9]*-[0-9]*.shtml",
		"http://www.ha.chinanews.com.cn/GNnews/[0-9]*/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.shtml",
		"http://www.sc.chinanews.com/news/[0-9]*/[0-9]*/[0-9]*.html",
		"http://www.jx.chinanews.com/[0-9\\/]+.html"
	};
	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		news.setUrl(url);		
		String title=null;
		String content=null;
		Long date=null;
		String author=null;
		Document doc=Jsoup.parse(htmltxt);
		if(url.startsWith("http://www.sc.chinanews.com.cn/") || url.startsWith("http://www.sc.chinanews.com/")){
			title =doc.select("div.c_title").text();
			if(StringUtils.isBlank(title))
				title=doc.select("h1").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div#txt_content"));
			if(StringUtils.isBlank(content)){
				String st=null;
				st= doc.select("textarea.hidden").text();
				content=HtmlCleaner.getContentHtml(url,Jsoup.parse(st).select("h2"));
			}
			author=doc.select("div.f_l").text();
			author=StringUtils.substringBetween(author, " 来源：", " 编辑：");
			date=DateUtils.matchDate(doc.select("div.f_l").text());
			if(date == null)
				date=DateUtils.matchDate(doc.select(".nph_photo_ctrl").text());
		}else if(url.startsWith("http://www.ha.chinanews.com.cn/")){
			title =doc.select("div.f18px").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("table.f14px"));
			date=DateUtils.matchDate(doc.select("div.f12px").text());
		}
		else if(url.startsWith("http://www.jx.chinanews.com/")){
			title =doc.select("h1.artical").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("div#artContent"));
			date=DateUtils.matchDate(doc.select("div.info").text());
		}

		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);	
		news.setAuthor(author);
		
		return news;
	}
	
	public static void main(String[] args) {
		ScchinanewsNewsAnalyse analyse=new ScchinanewsNewsAnalyse();
		String url="http://www.jx.chinanews.com/2015/1207/1923093.html";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
